package com.wkf.calculator.utils;

import com.wkf.calculator.*;

import java.util.ArrayList;

public class CountScanInfo {
    public static float countScanInfo(String tmp){
        int j = 0;
        int i = -1;
        float result = 0;
        ArrayList totalBox = GetScanInfos.getScanInfos(tmp);
        ArrayList numBox = (ArrayList) totalBox.get(0);
        ArrayList symbolBox = (ArrayList) totalBox.get(1);
        for (int k = 0; k < symbolBox.size(); k++){
            if ((char)symbolBox.get(k) == '*' || (char)symbolBox.get(k) == '/'){
                if (i == -1){
                    i = k;
                }
                switch ((char)symbolBox.get(k)){
                    case '*':
                        IMethodFactory multiplyfactory = new MultiplyFactory();
                        numBox.set(i,multiplyfactory.getMethod().getCount((float)numBox.get(i),(float)numBox.get(i+1)));
                        numBox.remove(i+1);
                        result = (float)numBox.get(i);
                        break;
                    case '/':IMethodFactory divisionfactory = new DivisionFactory();
                        numBox.set(i,divisionfactory.getMethod().getCount((float)numBox.get(i),(float)numBox.get(i+1)));
                        numBox.remove(i+1);
                        result = (float)numBox.get(i);
                        break;
                }
            }
        }
        for (int k = 0; k < symbolBox.size(); k++){
            switch ((char)symbolBox.get(k)){
                case '+':IMethodFactory plusfactory = new PlusFactory();
                    numBox.set(j,plusfactory.getMethod().getCount((float)numBox.get(j),(float)numBox.get(j+1)));
                    numBox.remove(j+1);
                    result = (float)numBox.get(j);
                    break;
                case '-':IMethodFactory minusfactory = new MinusFactory();
                    numBox.set(j,minusfactory.getMethod().getCount((float)numBox.get(j),(float)numBox.get(j+1)));
                    numBox.remove(j+1);
                    result = (float)numBox.get(j);
                    break;
            }
        }
        return result;
    }
}
